﻿#include "SystemJournalCore.h"
#include <QMutexLocker>
#include <QDateTime>

#include "journal_log.h"

#define queue_length (50)

SystemJournalCore::SystemJournalCore()
{
}

SystemJournalCore::~SystemJournalCore()
{

}
static SystemJournalCore* instance = nullptr;
SystemJournalCore* SystemJournalCore::GetInstance()
{
	if (instance == nullptr) {
		instance = new SystemJournalCore();
	}
	return instance;
}

void SystemJournalCore::WriteTraceJournal(JournalType type, const QString& data)
{
	QString str = QDateTime::currentDateTime().toString("MM-dd hh:mm:ss ") + data;
	switch (type) {
	case JournalType::journal_type_trace:
	{
		QMutexLocker locker(&trace);
		if (m_trace.size() > queue_length) {
			m_trace.clear();
		}
		m_trace.push_back(str);
		log_info("{}", str);
	}
	break;
	case JournalType::journal_type_warning:		
	{
		QMutexLocker locker(&warn);
		if (m_warnning.size() > queue_length) {
			m_warnning.clear();
		}
		m_warnning.push_back(str);
		log_error("{}", str);
	}
		break;
	case JournalType::journal_type_error:	
	{
		QMutexLocker locker(&error);
		if (m_error.size() > queue_length) {
			m_error.clear();
		}

		m_error.push_back("<font size=4 color=red> " + str +" </font>");
		log_error("{}", str);
	}
		break;
	default:
		break;
	}
}

QStringList SystemJournalCore::ReadTraceJournal(JournalType type)
{
	QStringList str;

	switch (type) {
	case JournalType::journal_type_trace:
	{
		QMutexLocker locker(&trace);
		str = m_trace;
		m_trace.clear();
	}
	break;
	case JournalType::journal_type_warning:
	{
		QMutexLocker locker(&warn);
		str = m_warnning;
		m_warnning.clear();
	}
	break;
	case JournalType::journal_type_error:
	{
		QMutexLocker locker(&error);
		str = m_error;
		m_error.clear();
	}
	break;
	default:
		break;
	}

	return str;
}
